<?php

/**
 * This is the model class for table "messages".
 *
 * The followings are the available columns in table 'messages':
 * @property integer $id
 * @property integer $board_id
 * @property integer $parent_id
 * @property string $subject
 * @property string $content
 * @property integer $author_id
 * @property integer $replies_count
 * @property integer $last_reply_id
 * @property string $created_on
 * @property string $updated_on
 * @property integer $is_locked
 * @property integer $sticky
 *
 * The followings are the available model relations:
 * @property Boards[] $boards
 * @property Board $board
 * @property User $author
 */
class Message extends CActiveRecord
{
    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Messages the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'messages';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('board_id, created_on, updated_on', 'required'),
            array('board_id, parent_id, author_id, replies_count, last_reply_id, is_locked, sticky', 'numerical', 'integerOnly'=>true),
            array('subject', 'length', 'max'=>255),
            array('content', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, board_id, parent_id, subject, content, author_id, replies_count, last_reply_id, created_on, updated_on, is_locked, sticky', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'boards' => array(self::HAS_MANY, 'Board', 'last_message_id'),
            'board' => array(self::BELONGS_TO, 'Board', 'board_id'),
            'author' => array(self::BELONGS_TO, 'User', 'author_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'board_id' => 'Board',
            'parent_id' => 'Parent',
            'subject' => 'Subject',
            'content' => 'Content',
            'author_id' => 'Author',
            'replies_count' => 'Replies Count',
            'last_reply_id' => 'Last Reply',
            'created_on' => 'Created On',
            'updated_on' => 'Updated On',
            'is_locked' => 'is_locked',
            'sticky' => 'Sticky',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('board_id',$this->board_id);
        $criteria->compare('parent_id',$this->parent_id);
        $criteria->compare('subject',$this->subject,true);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('author_id',$this->author_id);
        $criteria->compare('replies_count',$this->replies_count);
        $criteria->compare('last_reply_id',$this->last_reply_id);
        $criteria->compare('created_on',$this->created_on,true);
        $criteria->compare('updated_on',$this->updated_on,true);
        $criteria->compare('is_locked',$this->is_locked);
        $criteria->compare('sticky',$this->sticky);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
}